#
# Author: pepone.onrez@gmail.com
#


SET ( ANTLR_COMMAND /opt/antlr/bin/antlr )
SET ( MOVE_FILE mv -v )


# Generate rules for antlr lexers

MACRO ( GENERATE_LEXER_RULES LEXER_GENERATED_SOURCE LEXER_GENERATED_HEADER LEXER_GENERATED_TOKENS_HEADER LEXER_GENERATED_TOKENS_TXT )

    # FileName for our lexer definition
    SET ( LEXER_ANTRL_SOURCE_BASENAME ${ARGN} )

    # Full path for our lexer definition
    SET ( LEXER_ANTRL_SOURCE_FILE ${PROJECT_SOURCE_DIR}/antlr/${LEXER_ANTRL_SOURCE_BASENAME} )

    #Generate filenames based on the lexer filename
    STRING( REGEX REPLACE "\\.g" .cpp LEXER_CPP_SOURCE_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\.g" .hpp LEXER_CPP_HEADER_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\Lexer.g" TokenTypes.hpp LEXER_TOKENS_CPP_HEADER_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\Lexer.g" TokenTypes.txt LEXER_TXT_TOKENS_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )

    #Files generated by antlr
    SET ( LEXER_CPP_SOURCE_FILE ${PROJECT_BINARY_DIR}/${LEXER_CPP_SOURCE_BASENAME} )
    SET ( LEXER_CPP_HEADER_FILE ${PROJECT_BINARY_DIR}/${LEXER_CPP_HEADER_BASENAME} )
    SET ( LEXER_TOKENS_CPP_HEADER_FILE ${PROJECT_BINARY_DIR}/${LEXER_TOKENS_CPP_HEADER_BASENAME} )
    SET ( LEXER_TXT_TOKENS_FILE ${PROJECT_BINARY_DIR}/${LEXER_TXT_TOKENS_BASENAME} )


    #Command that runs antlr for generating the lexer code
    ADD_CUSTOM_COMMAND(
        OUTPUT ${LEXER_CPP_SOURCE_FILE} ${LEXER_CPP_HEADER_FILE} ${LEXER_TOKENS_CPP_HEADER_FILE} ${LEXER_TXT_TOKENS_FILE}
        COMMAND ${ANTLR_COMMAND} ${LEXER_ANTRL_SOURCE_FILE}
        DEPENDS ${LEXER_ANTRL_SOURCE_FILE}
        COMMENT "-- Generating ${LEXER_CPP_SOURCE_FILE} ${LEXER_CPP_HEADER_FILE} ${LEXER_TOKENS_CPP_HEADER_FILE} ${LEXER_TXT_TOKENS_FILE} file from ${LEXER_ANTRL_SOURCE_BASENAME}")


    # Asign return values
    SET( LEXER_GENERATED_SOURCE ${LEXER_CPP_SOURCE_FILE} )
    SET( LEXER_GENERATED_HEADER ${LEXER_CPP_HEADER_FILE} )
    SET( LEXER_GENERATED_TOKENS_HEADER ${LEXER_TOKENS_CPP_HEADER_FILE} )
    SET( LEXER_GENERATED_TOKENS_TXT ${LEXER_TXT_TOKENS_FILE} )

ENDMACRO ( GENERATE_LEXER_RULES LEXER_GENERATED_SOURCE LEXER_GENERATED_HEADER LEXER_GENERATED_TOKENS_HEADER LEXER_GENERATED_TOKENS_TXT )
